<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>区域设置</title>
		
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!-- 导入ocupload -->
		<script type="text/javascript" src="../../js/ocupload/jquery.ocupload-1.1.2.js" ></script>
		
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css"/>
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css"/>
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/default.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/city-picker.css"/>
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		<script src="../../js/citypicker/city-picker.data.js" type="text/javascript"></script>
		<script src="../../js/citypicker/city-picker.js" type="text/javascript"></script>

		<script type="text/javascript">
			$(function(){
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({visibility:"visible"});
				//重置citypicker
                $("#reset").click(function() {
                    // 获取输入框，并在点击按钮后清空输入框中内容
                    $("#city-picker").citypicker("reset");
                });
                //查询生成拼音简写
				$("#areadiv").click(function () {
                   var areainfo =  $("#city-picker").val();
                   //排除无效点击，既省市区不全时数据
                  	var areas =  areainfo.split("/");
                  	if(areas.length < 3){
                  	    return;
					}
					//获取简码和城市编码
					$.post("../../area_info.action",{"areaInfo":areainfo},function (data) {
						if(data != null){
						   $("#shortcode").val(data.shortcode);
                            $("#citycode").val(data.citycode);
						}
                    });
                });
				// 区域管理数据表格
				$('#grid').datagrid( {
					iconCls : 'icon-forward',
					fit : true,
					border : false,
					rownumbers : true,
					striped : true,
					pageList: [30,50,100],
					pagination : true,
					toolbar : toolbar,
					url : "../../area_pageQuery.action",
					idField : 'id',
					columns : columns,
					onDblClickRow : doDblClickRow
				});
				
				// 添加、修改区域窗口
				$('#addWindow').window({
			        title: '添加修改区域',
			        width: 400,
			        modal: true,
			        shadow: true,
			        closed: true,
			        height: 400,
			        resizable:false
			    });
			    
				// 为导入按钮，添加一键上传效果 
				$("#button-import").upload({
					// 默认name为file
					action:'../../area_batchImport.action',
					onSelect:function(){
						// 选中文件后，关闭自动提交
						this.autoSubmit = false;
						
						// 判定文件格式 ，以.xls 或者 .xlsx 结尾
						var filename = this.filename();
						var regex = /^.*\.(xls|xlsx)$/;
						if(regex.test(filename)) {
							// 满足
							this.submit();
						}else {
							$.messager.alert("警告","只能上传.xls或.xlsx结尾的文件！","warning");	
						}
					},
					onComplete:function(response){
						alert("文件上传成功！");
					}
				});
				
				// 为查询提交按钮 ，添加点击事件
				$("#searchBtn").click(function(){
					// 将查询表单内容转换为 json
					var params = $("#searchForm").serializeJson();
					
					// 绑定参数到datagrid
					$("#grid").datagrid('load',params);
					
					// 关闭查询窗口
					$("#searchWindow").window('close');
				});

                // 保存新增区域
                $('#save').click(function(){
                    // 判断form元素 是否满足 校验规则
                    if( $("#areaForm").form('validate') ) {
                        $("#areaForm").submit();
                    } else {
                        $.messager.alert("警告","表单中存在非法输入项！","warning");
                    }
                });

			});
			
			// 将form请求数据转换为json对象
			$.fn.serializeJson = function(){  
	            var serializeObj={};  
	            var array=this.serializeArray();  
	            var str=this.serialize();  
	            $(array).each(function(){  
	                if(serializeObj[this.name]){  
	                    if($.isArray(serializeObj[this.name])){  
	                        serializeObj[this.name].push(this.value);  
	                    }else{  
	                        serializeObj[this.name]=[serializeObj[this.name],this.value];  
	                    }  
	                }else{  
	                    serializeObj[this.name]=this.value;   
	                }  
	            });  
	            return serializeObj;  
	        };
	        
			// 定义列
			var columns = [ [ {
				field : 'id',
				checkbox : true,
			},{
				field : 'province',
				title : '省',
				width : 120,
				align : 'center'
			}, {
				field : 'city',
				title : '市',
				width : 120,
				align : 'center'
			}, {
				field : 'district',
				title : '区',
				width : 120,
				align : 'center'
			}, {
				field : 'postcode',
				title : '邮编',
				width : 120,
				align : 'center'
			}, {
				field : 'shortcode',
				title : '简码',
				width : 120,
				align : 'center'
			}, {
				field : 'citycode',
				title : '城市编码',
				width : 200,
				align : 'center'
			} ] ];
			
			//工具栏
			var toolbar = [ {
				id : 'button-edit',	
				text : '修改',
				iconCls : 'icon-edit',
				handler : doView
			}, {
				id : 'button-add',
				text : '增加',
				iconCls : 'icon-add',
				handler : doAdd
			}, {
				id : 'button-delete',
				text : '删除',
				iconCls : 'icon-cancel',
				handler : doDelete
			}, {
				id : 'button-import',
				text : '导入',
				iconCls : 'icon-redo'
			}, {
				id : 'button-search',
				text : '查询',
				iconCls : 'icon-search',
				handler : function() {
					$("#searchWindow").window('open');
				}
			}];
			
			//----点击工具栏中按钮要执行的方法----开始
			function doAdd(){
			    //清除表单数据
                $(":input","#areaForm").not(":button",":reset","hidden","submit").val("");
                //清除city-pikper数据
                $("#city-picker").citypicker("reset");
				$('#addWindow').window("open");
			}

			//修改
			function doView(){
                // 获取当前datagrid选中数据
                var rows = $("#grid").datagrid('getSelections');
                if(rows.length != 1) {
                    // 没选 或 多选
                    $.messager.alert("提示信息","修改数据时，只能选中一行","warning");
                }else {
                    // 只选中一行
                    var row = rows[0];
                    // 进行表单回显操作
                    $("#areaForm").form('load', row);
                    //进行city-picker回显
                    $("#city-picker").citypicker();
                        // 赋值前，必须先执行 reset、destroy
                        $('#city-picker').citypicker('reset');
                        $('#city-picker').citypicker('destroy');
                        $('#city-picker').citypicker({
                            province: row.province,
                            city: row.city,
                            district:row.district
                    });
                    // 显示窗口
                    $("#addWindow").window('open');
                }
			}
			//删除
			function doDelete(){
				//获取勾选的数据
                var rows = $("#grid").datagrid('getSelections');
                if (rows.length == 0) {
                    // 没有选中数据
                    $.messager.alert("警告","作废快递员必须选中一条以上数据","warning");
                } else {
                    // 选中数据 ，获取选中所有id ，拼接字符串方法
                    var array = new Array();
                    for(var i = 0; i < rows.length; i++) {
                        array.push(rows[i].id);
                    }

                    // 生成字符串
                    var ids = array.join(",");

                    // 将字符串发送服务器
                    window.location.href = "../../area_delete.action?ids=" + ids;
                }
			}
			//----点击工具栏中按钮要执行的方法----结束
			
			function doDblClickRow(){
				alert("双击表格数据...");
			}
		</script>
	</head>

	<body class="easyui-layout" style="visibility:hidden;">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
		<div class="easyui-window" title="区域添加修改" id="addWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div region="north" style="height:31px;overflow:hidden;" split="false" border="false">
				<div class="datagrid-toolbar">
					<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
				</div>
			</div>

			<div region="center" style="overflow:auto;padding:5px;" border="false">
				<form id="areaForm" action="../../area_save.action" method="post">
					<table class="table-edit" width="100%" align="center">
						<tr class="title">
							<td colspan="2">区域信息
								<input type="hidden" name="id" />
							</td>
						</tr>
						<tr>
							<td>省</td>
							<td>
								<div id="areadiv" style="position: relative;"><!-- 必须加div，不然不显示弹窗 -->
								<input name="areaInfo" id="city-picker" readonly type="text" data-toggle="city-picker" >
								<input id="reset" type="button" value="重置"/>
								</div>
							</td>
						</tr>
						<tr>
							<td>邮编</td>
							<td>
								<input type="text" name="postcode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>简码</td>
							<td>
								<input type="text" id="shortcode" name="shortcode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>城市编码</td>
							<td>
								<input type="text" id="citycode" name="citycode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		
		<!-- 查询区域-->
		<div class="easyui-window" title="查询区域窗口" closed="true" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="width: 400px; top:40px;left:200px">
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="searchForm">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">查询条件</td>
						</tr>
						<tr>
							<td>省份</td>
							<td>
								<input type="text" name="province" />
							</td>
						</tr>
						<tr>
							<td>城市</td>
							<td>
								<input type="text" name="city" />
							</td>
						</tr>
						<tr>
							<td>区（县）</td>
							<td>
								<input type="text" name="district" />
							</td>
						</tr>
						<tr>
							<td colspan="2"><a id="searchBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</body>

</html>